<?php

class dev_config 
{ 
	public static function local()
	{
		self::apply('local');
	}

	public static function test()
	{
		self::apply('test');
	}

	public static function apply ($type) 
	{ 
		$dir = 'config/'.$type.'/'; 
		$files = Kohana::list_files ($dir); 
		foreach ($files as $file) { 
			$config = array(); 
			$filename = basename($file); 
			if (preg_match ('/(.*)?.php$/', $filename, $regs)) 
			{ 
				$name = $regs[1]; 
				if ($name == 'config') 
					$name = 'core'; 
				include $file; 
				foreach ($config as $key=>$setting) 
				{ 
					Kohana::config_set($name.'.'.$key, $setting); 
				}
			} 
		} 
	}
} 

if(defined('IN_CRON'))
{
	switch(IN_CRON)
	{
		case 'local':
		Event::add('system.ready', array('dev_config', 'local'));
		break;
		case 'test':
		Event::add('system.ready', array('dev_config', 'test'));
		break;
	}
}
else
{
	if($_SERVER['SERVER_NAME']=='localhost' OR $_SERVER['SERVER_NAME']=='www.test.com')
		Event::add('system.ready', array('dev_config', 'local'));
}

// functions
function rmdirr($dirname)
{
	if (!file_exists($dirname)) 
	{
		return false;
	}
	if (is_file($dirname) || is_link($dirname)) 
	{
		return unlink($dirname);
	}
	$dir = dir($dirname);
	while (false !== $entry = $dir->read()) 
	{
		if ($entry == '.' || $entry == '..') 
		{
			continue;
		}
		rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
	}
	$dir->close();
	return rmdir($dirname);
}

function nl2br2($string) {
	$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
	return $string;
} 

function convert_to_url($text)
{
	$text = preg_replace("/([a-zA-Z]+:\/\/[a-z0-9\_\.\-]+"."[a-z]{2,6}[a-zA-Z0-9\/\*\-\_\?\&\%\=\,\+\.]+)/"," <a href=\"$1\" target=\"_blank\">$1</a>", $text);
	$text = preg_replace("/[^a-z]+[^:\/\/](www\."."[^\.]+[\w][\.|\/][a-zA-Z0-9\/\*\-\_\?\&\%\=\,\+\.]+)/"," <a href=\"$1\" target=\"_blank\">$1</a>", $text);
	return $text;
}

function error_403()
{
	message::error(Kohana::lang('error.need_login',url::site('user/login').'?return_url='.url::site(url::current())));
	Kohana::show_404(false,'error');
	exit;
}

function error_404()
{
	Kohana::show_404(false,'error');
	exit;
}

function post_request($url, $data, $optional_headers = null)
{
	$params = array('http' => array(
		'method' => 'POST',
		'content' => http_build_query($data),
		));
	if ($optional_headers !== null) 
	{
		$params['http']['header'] = $optional_headers;
	}
	$ctx = stream_context_create($params);
	$fp = @fopen($url, 'rb', false, $ctx);
	if (!$fp) 
	{
	   throw new Exception("Problem with $url");
	}
	$response = @stream_get_contents($fp);
	if ($response === false) 
	{
	   throw new Exception("Problem reading data from $url");
	}
	return $response;
}

function welcome()
{
	$time = date('H');
	$message = '你好';
	if($time>0 AND $time<6)
	{
		$message = '快点睡吧';
	}
	if($time>=6 AND $time<11)
	{
		$message = '上午好';
	}
	if($time>=11 AND $time<14)
	{
		$message = '中午好';
	}
	if($time>=14 AND $time<18)
	{
		$message = '下午好';
	}
	if($time>=18 AND $time<=24)
	{
		$message = '晚上好';
	}
	return $message;
}


// add events hook
Event::add('system.display',array('javascript','render'));
if(IN_PRODUCTION)
{
	Event::clear('system.404', array('Kohana', 'show_404'));
	Event::add('system.404', 'error_404');
}

if(in_array('mod_rewrite',apache_get_modules()))
{
	Kohana::config_set('core.index_page','');
}

$site_domain = $_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/';
Kohana::config_set('core.site_domain',$site_domain);
